package com.cps116.parcelables;

import java.util.ArrayList;
import java.util.List;

import com.cps116.friendstracker.FriendsFinderMapActivity;
import com.cps116.friendstracker.MapPathOverlay;
import com.cps116.friendstracker.R;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.OverlayItem;


import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

public class LocationTraceList implements MapParcelable{

	private List<MapParcelableData> dataList;

	
	public LocationTraceList(List<MapParcelableData> data_list){
		dataList = data_list;
		if (dataList == null)
			dataList = new ArrayList<MapParcelableData>();
	}
	

	public void addMapParcelable(MapParcelableData element){
		if(dataList == null)
			dataList = new ArrayList<MapParcelableData>();
		
		dataList.add(element);
	}
	
	
	public int describeContents() {
		//no need to mess around with this class...
		return 0;
	}
	

	public void writeToParcel(Parcel dest, int flags) {
		Log.d("LocationTraceList", "LocationTraceList writeToParcel starts");
		//write the list for the data
		dest.writeTypedList(dataList);
	}


	public void readFromParcel(Parcel in) {
		dataList = new ArrayList<MapParcelableData>();
		Log.d("LocationTraceList", "LocationTraceList read starts");
		in.readTypedList(dataList, MapParcelableData.CREATOR);
	}


	//create the PARCELABLE CREATOR CLASS
	public static final Parcelable.Creator<LocationTraceList> CREATOR
	= new Parcelable.Creator<LocationTraceList>() {
		
		public LocationTraceList createFromParcel(Parcel in) {
			return new LocationTraceList(in);
		}

		public LocationTraceList[] newArray(int size) {
			return new LocationTraceList[size];
		}
	};	


	private LocationTraceList(Parcel in){
		readFromParcel(in);
	}


	public List<MapParcelableData> getDataList() {
		return dataList;
	}

	
	/**
	 * Performs the overlay addition to the map...
	 */
	public void operateOnMapActivity(FriendsFinderMapActivity mapActivity) {
		//specify how the map is to be mapped out here...
		//start asynctask to perform the actual plotting on the map for just the current user
		for(MapParcelableData overlayData: dataList){
			new OverlayCreater(mapActivity, overlayData).execute();
		}
	}


	//CHECK HERE FOR CORRECTNESS...
	private class OverlayCreater extends AsyncTask<String, String, MapPathOverlay> {

		private FriendsFinderMapActivity mapActivity;
		private MapParcelableData userLocParcelable;
		
		public OverlayCreater(FriendsFinderMapActivity mapActivity, MapParcelableData userLocParcelable){
			this.mapActivity = mapActivity;
			this.userLocParcelable = userLocParcelable;
		}


		@Override
		protected MapPathOverlay doInBackground(String... string) {
			
			Log.d("OverlayCreater", "starting async task");
			//create drawable object (TODO: create a drawable object with nametags... that's too hard) 
			//add it to the overlay...
			Drawable icon = mapActivity.getResources().getDrawable(R.drawable.flag_1);
			MapPathOverlay overlay = new MapPathOverlay(icon, mapActivity);
			Log.d("OverlayCreater", "created overlay object");
			
			//populate the the individual 
			for(Coordinate coord: userLocParcelable.getCoords()) {
				int lat_micro = (int) (1e6 * coord.getLatCoord());
				int long_micro = (int) (1e6 * coord.getLongCoord());
				GeoPoint gp = new GeoPoint(lat_micro, long_micro);
				OverlayItem item = new OverlayItem(gp, "", "");
				overlay.addOverlay(item);
			}
			
			return overlay;
		}


		@Override
		protected void onPostExecute(MapPathOverlay overlay) {
			super.onPostExecute(overlay);
			
			//get the user with the current overlay with the user_string...for update purposes if needed..
			String user_name = userLocParcelable.getUserName();
			//get update interval in milliseconds
			long update_time = userLocParcelable.getUpdateTimeInMillis();
			//check if the user is updating
			boolean is_updating = userLocParcelable.isContinuous();
			//get the userid
			String u_id = userLocParcelable.getUserId();
			//add all these information to FriendsFinderMapActivity
			mapActivity.addOverlay(overlay, user_name, u_id, is_updating);
			
			//TODO: fix this part...where we center the result...ideally you want to center on the user...
			GeoPoint pt	 =	overlay.getLastGeopoint();
			MapController controller = mapActivity.getMapControl();
			controller.setCenter(pt);
		}
	}

}
